$(document).ready(function(){
    var sErrorAdd =' Error  : Please Fill in Name Catalog';
    var sErrorAddNotID ='Error : Please Click Add New';
    var sErrorNotID = 'Error : Please chosen 1 Item';
    var sErrorproduct = 'Error : Please chosen 1 Item';
    var sErrorproductRow = 'Error : Max 6 Product Hot, Please delete product to add';
    var sErrorproductExit = 'Error : Product exits in list';

    $('#bt_updatecatalog').click(function(){
        var iIDCatalog = $('#tb_catalog_id').val();
        var sCatalogy = $('#tb_catalog_name').val();
        var iCatalogygenid = $("select").val();
        var sIDcatalogygen = $('#listhm option:selected').text();
        if(iIDCatalog != '' && sCatalogy != '')
        {
            $.post('~admin/updateCatalogy',{IDcatalog : iIDCatalog,idcataloggen : iCatalogygenid, catalog : sCatalogy} , function(data){
                if(data.indexOf('OK') != -1){
                    $('#tb_catalog_id').val('');
                    $('#tb_catalog_name').val('');
                    if(row != -1)
                    {
                        var table = document.getElementById("tableDisplayCatalog");
                        var rowtb=table.rows[row+1];
                        // alert(rowtb.cells[0].innerHTML);
                        rowtb.cells[1].innerHTML=sCatalogy;
                        rowtb.cells[2].innerHTML=sIDcatalogygen;
                    }
                    $('#img_catalo').show();
                    $('#lb_error_catalo').hide();
                }
            });
        }
        else
        {
            $('#img_catalo').hide();
            $('#lb_error_catalo').show();
            if(iIDCatalog == '')
            {
                $('#lb_error_catalo').html(sErrorNotID);
            }
            else
            {
                if(sCatalogy == '')
                    $('#lb_error_catalo').html(sErrorAdd);
            }
        }
    });
    $('#bt_deletecatalog').click(function(){
        var iIDCatalog = $('#tb_catalog_id').val();
        if(iIDCatalog !='')
        {
            $.post('~admin/deleteCatalogy',{IDcatalog : iIDCatalog} , function(data){
                if(data.indexOf('OK') != -1){
                    $('#tb_catalog_id').val('');
                    $('#tb_catalog_name').val('');
                    if(row != -1)
                    {
                        $('#tableDisplayCatalog').find('tr#'+iIDCatalog).remove();  
                    }
                    $('#img_catalo').show();
                    $('#lb_error_catalo').hide();
                };
            });
        }
        else
        {
            $('#img_catalo').hide();
            $('#lb_error_catalo').show()
            $('#lb_error_catalo').html(sErrorNotID);
        }
    }); 
    // Script Hot product
    
    $('#tablehotproduct').on( "click",'tr', function() {
        //var val = $(this).find('td').eq(0).text();
        var catalog_id = $(this).find('td').eq(0).text();
        var catalog = $(this).find('td').eq(1).text();
        $('#tb_id_product').val(catalog_id);
        $('#tb_name_product').val(catalog);
        $('#img_product').hide();
        $('#lb_error_product').hide();
    });
    $('#project').click(function(){
        $('#img_product').hide();
        $('#lb_error_product').hide();
    });
    $('#bt_cleart').click(function(){
        $('#project').val('');
        $('#lb_error_product').hide();
    });
    $('#lb_add_producthot').click(function(){
        $('#tb_name_product').val('');
        $('#tb_id_product').val('');
        $('#hotproduct').show();
        $('#bt_addexit_producthot').show();
        $('#lb_add_producthot').hide();
    });
    $('#bt_addexit_producthot').click(function(){

        $('#bt_addexit_producthot').hide();
        $('#lb_add_producthot').show();
        $('#hotproduct').hide();
    });
    $('#bt_delete_producthot').click(function(){
        var iIDCatalog = $('#tb_id_product').val();
        if(iIDCatalog !='')
        {
            $.post('~admin/deleteHotProduct',{IDcatalog : iIDCatalog} , function(data){
                if(data.indexOf('OK') != -1){
                    $('#tb_id_product').val('');
                    $('#tb_name_product').val('');
                    if(row != -1)
                    {
                        $('#tablehotproduct').find('tr#'+iIDCatalog.trim()).remove();  
                    }
                    $('#img_product').show();
                    $('#lb_error_product').hide();
                };
            });
        }
        else
        {
            $('#img_product').hide();
            $('#lb_error_product').show()
            $('#lb_error_product').html(sErrorNotID);
        }
    });
    $('#bt_addproducthot').click(function(){
        var id = $('#IDObject').text();
        var name = $('#project-id').val();
        var catalog = $('#CatalogObject').text();
        var dongia = $('#DongiaObject').text();
        var table = document.getElementById("tablehotproduct");
        var rowCount = table.rows.length;
        if(id != '' && name !='' && rowCount<7)
        {
            $.post('~admin/checkAddHotProduct',{idProduct : id} , function(data){
                if(data.indexOf('Fail') == -1){
                    $('#project').val('');
                    $('#lb_error_product').show();
                    $('#lb_error_product').html(sErrorproductExit);
                }
                else
                {
                    $.post('~admin/addHotProduct',{idProduct : id} , function(data){
                        if(data.indexOf('Fail') == -1){
                            $('#project-id').val('');
                            $('#project').val('');
                            var trTable = '<tr id="'+id.trim()+'"><td>'+id+'</td><td>'+name+'</td><td>'+catalog+'</td><td>'+dongia+'</td></tr>';
                            $('#tablehotproduct').append(trTable);
                            $('#img_product').show();
                            $('#lb_error_product').hide();
                        }
                    });
                }
            });
        }
        else
        {
            $('#lb_error_product').show();
            $('#project').val('');
            if(rowCount>=7)
            {
                $('#lb_error_product').html(sErrorproductRow);
            }
            else
            {
                $('#img_product').hide();
                $('#lb_error_product').html(sErrorproduct);
            }

        }

    });
    
    // Auto Complete 
    if($('#tb_data').length > 0){
        var data_product=$('#tb_data').val();
        var ObjectInfor =data_product.split('||');
        var data=new Array(); 
        for (var i=0,len=ObjectInfor.length; i<len-1; i++)
        {
            var detalObject=ObjectInfor[i].split('-');
            data.push({
                id : detalObject[0],
                label: detalObject[1],
                value: detalObject[1],
                dongia: detalObject[2],
                catalog: detalObject[3]
            });
        }
        $(function() {
            $( "#project" ).autocomplete({
                appendTo: '#result_panel',
                minLength: 0,
                source: data,
                focus: function( event, ui ) {
                    $( "#project" ).val( ui.item.label );
                    return false;
                },
                select: function( event, ui ) {
                    $( "#project" ).val( ui.item.label );
                    $( "#project-id" ).val( ui.item.value );
                    $( "#CatalogObject" ).html( ui.item.catalog );
                    $( "#IDObject" ).html( ui.item.id );
                    $( "#DongiaObject" ).html( ui.item.dongia );

                    return false;
                }
            })
            .data("ui-autocomplete" )._renderItem = function( ul, item ) {
                return $( '<li>' )
                .append( '<a>'+item.value+'<p>   Đơn giá :' + item.dongia +' Đơn giá :'+item.catalog + '</p></a>' )
                .appendTo( ul );
            };

        });
    }

});
